package cn.com.generaldata.os.ws.impl.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jasig.cas.client.util.CommonUtils;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.dao.impl.ReportDAO;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.pojo.Report;
import cn.com.generaldata.os.result.ReportVersionYearRangeResult;
import cn.com.generaldata.os.util.CacheManager;
import cn.com.generaldata.os.vo.ReportVO;
import cn.com.generaldata.os.vo.ResultVO;
import cn.com.generaldata.os.vo.UserVO;

@Component
@InjectName
public class ReportWebAction extends ReportAction {

	@Autowired
	private CacheManager cacheManager;
	
	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<Report> reportDAO;

	/**
	 * 获取已提交报表清单
	 * @param checkStatus  状态： 1、已提交   3、审核未通过     5、终审通过
	 * */
	@At("/company/report/list/?")
	@Ok("jsp:/static/company/report/list")
	public ResultVO<ReportVO> reportList(int checkStatus, @Param("reportProxyId") String reportProxyId,
			@Param("typeId") String reportSystem, @Param("reportTypeId") String reportTypeCode,
			@Param("versionId") String reportVersion, @Param("searchDate") Date startingReportDate,
			@Param("endDate") Date endingReportDate, @Param("pageNum") Integer pageNum,
			@Param("reportNo") String typeNo, HttpServletRequest request) {

		UserVO user = (UserVO) request.getSession().getAttribute("user");

		String unitId = user.getUserUnitId();
		String userId = user.getId();
		String userAreaId = user.getUserAreaId();
		reportProxyId = userId;

		//查询当前用户涉及的报表制度
		//request.setAttribute("sysCodeList", queryTypeNew(unitId));
		request.setAttribute("sysCodeList", this.queryType());

		//报表状态
		request.setAttribute("checkStatus", checkStatus);
		if (reportSystem == null) {
			return null;
		}

		//获取频率类型信息
		request.setAttribute("reportTimetype", cacheManager.getReportTimetype());
		if (pageNum == null) {
			pageNum = new Integer(1);
		}
		if (CommonUtils.isNotBlank(typeNo)) {
			//			try {
			//				typeNo = new String(typeNo.getBytes("ISO8859-1"), "utf-8");
			//			} catch (UnsupportedEncodingException e) {
			//				// TODO Auto-generated catch block
			//				e.printStackTrace();
			//			}
			request.setAttribute("typeNo", typeNo);
		}

		return queryReportsByParentForWebUnit(reportProxyId, userId, (short) checkStatus, reportSystem, reportTypeCode,
				reportVersion, startingReportDate, endingReportDate, userAreaId, pageNum, typeNo);
	}

	/**
	 * 获取海洋局用户审核列表
	 * @param checkStatus  1初审    55终审
	 * */
	@At("/audit/webList/?")
	@Ok("jsp:/static/ocean/audit/webList")
	public ResultVO<ReportVO> firstTrial(Short checkStatus, @Param("reportProxyId") String reportProxyId,
			@Param("typeId") String reportSystem, @Param("reportTypeId") String reportTypeCode,
			@Param("versionId") String reportVersion, @Param("searchDate") Date startingReportDate,
			@Param("endDate") Date endingReportDate, @Param("p") Integer pageNum,
			@Param("createYear") String createYear, @Param("unitName") String unitName, HttpServletRequest request,
			HttpSession session) {

		UserVO user = (UserVO) request.getSession().getAttribute("user");

		String unitId = user.getUserUnitId();
		String userId = user.getId();
		String userAreaId = user.getUserAreaId();
		reportProxyId = userId;

		//查询当前用户涉及的报表制度
		request.setAttribute("sysCodeList", this.queryType());
		//报表状态
		request.setAttribute("checkStatus", checkStatus);

		if (createYear == null && reportSystem == null) {
			Calendar cal = Calendar.getInstance();
			String curYear = String.valueOf(cal.get(Calendar.YEAR));
			request.setAttribute("curYear", curYear);
			createYear = curYear;
		}

		ReportVersionYearRangeResult yearRange = getReportVersionYearRange();
		int startYear = yearRange.getStartYear();
		int endYear = yearRange.getEndYear();
		List<Integer> yearList = new ArrayList<Integer>();
		for (int i = endYear; i >= startYear; i--) {
			yearList.add(i);
		}
		request.setAttribute("yearList", yearList);
		//获取频率类型信息
		request.setAttribute("reportTimetype", cacheManager.getReportTimetype());
		if (pageNum == null) {
			pageNum = new Integer(1);
		}
		String tunitName = "";
		if (unitName == null) {
			tunitName = "";
		} else {
			tunitName = unitName;
		}
		return queryReportsByProxy(reportProxyId, userId, checkStatus, reportSystem, reportTypeCode, reportVersion,
				startingReportDate, endingReportDate, userAreaId, pageNum, null, createYear, tunitName);
	}
	
	@At("/report/updateReportDate/?/?/?")
	@Ok("json")
	public String updateReportDate(String reportId, String year, String month){
		
		try{
			Report report = reportDAO.get(Report.class, reportId);
			
			report.setReportMonth(year + "/" + month);
			report.setMonth(Integer.parseInt(month));
			
			reportDAO.update(report);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		
		return "sussess";
	}
	
	
	@At("/report/showReportInfo/?")
	@Ok("json")
	public Report showReportInfo(String reportId){
		
		Report report = reportDAO.get(Report.class, reportId);
		
		report.setArea(null);
		report.setUser(null);
		report.setOperator(null);
		
		return report;
	}
}
